//
//  PayViewController.swift
//  BookStore
//
//  Created by JinYuXin on 2022/5/12.
//

import UIKit

class PayViewController: UIViewController {

    @IBOutlet weak var totalPrice: UILabel!
    
    @IBOutlet weak var back: UIButton!
    @IBOutlet weak var ifpayed: UILabel!
    var price:String=""
    var bookids=[String]()
    var userid:String=""
    var historylistid:String=""
    var time:String=""
    var booknum:String=""
    override func viewDidLoad() {
        super.viewDidLoad()
        totalPrice.text = price
        HistoryTable.initTable()
    }
    
    @IBAction func payDid(_ sender: UIButton) {
        UserShopCarTable.deleteUserShopCar(userid)
        getId()
        getTime()
        let bookidsstr = bookids.joined(separator: ",")
        let history = HistoryList(historyid:historylistid,userid:userid,listtime:time,bookids:bookidsstr,num: booknum,price: price)
        HistoryTable.InsertHistoryTable(historylist: history)
        let controller:PayDidViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "paysucceed") as! PayDidViewController
        //self.dismiss(animated: true, completion: nil)
        self.present(controller, animated: true, completion: nil)
    }
    
    @IBAction func back(_ sender: Any) {
        
        let controller:MainViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "vcMain") as! MainViewController
        UIApplication.shared.windows[0].rootViewController = controller
    }
    
    //生成一个订单编号
    func getId()
    {
        let num:Int = Int(arc4random())
        let stnum = String(num)
        let time = Date().timeStamp
        historylistid = userid + stnum + time
        
    }
    func getTime()
    {
        let currentdate = Date()
        let dataFormatter = DateFormatter()
        dataFormatter.locale = Locale.current
        dataFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        let stringDate = dataFormatter.string(from: currentdate)
        time = stringDate
        
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
